package com.vm.activity;

import java.util.ArrayList;
import java.util.List;

import com.vm.adapter.FragAdapter;
import com.vm.fragment.CategoryAppFragment;
import com.vm.fragment.CategoryGameFragment;
import com.wwtscapp.apk.R;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class CategoryActivity extends FragmentActivity {
	RadioGroup nav_radiogroup;
	RadioButton rbtn_category_apply;
	RadioButton rbtn_category_game;
	ImageView imgbtn_search_back;
	private ViewPager vp;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.category);
		init();
		setView();
	}

	public void init() {
		nav_radiogroup = (RadioGroup) findViewById(R.id.nav_radiogroup);
		nav_radiogroup.setOnCheckedChangeListener(group_listener);
		imgbtn_search_back = (ImageView) findViewById(R.id.imgbtn_search_back);
		imgbtn_search_back.setOnClickListener(btn_backListener);
	}

	OnClickListener btn_backListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			onBackPressed();
		}
	};

	public void onBackPressed() {
		super.onBackPressed();
	}

	public OnCheckedChangeListener group_listener = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {

			try {
				String count = Integer.toString(vp.getAdapter().getCount());

				Log.i("vp中的fragment个数", count);
				switch (checkedId) {
				case R.id.rbtn_category_apply:
					vp.setCurrentItem(0);
					break;
				case R.id.rbtn_category_game:
					vp.setCurrentItem(1);
					break;

				}
			} catch (Exception e) {
				// TODO Auto-generated catch block

				vp.getAdapter();
				Log.e("vp错误", e.getMessage());
			}
		}
	};

	private void setView() {
		List<Fragment> fragments = new ArrayList<Fragment>();
		fragments.add(new CategoryAppFragment());
		fragments.add(new CategoryGameFragment());

		vp = (ViewPager) findViewById(R.id.viewpager);
		FragAdapter adapter = new FragAdapter(getSupportFragmentManager(),
				fragments);
		vp.setAdapter(adapter);

		// 设置最大缓存数
		// 默认缓存2个页面,当出现第三个页面会删掉之前缓存中的一个.
		vp.setOffscreenPageLimit(2);
		vp.setCurrentItem(0);

	}
}